// 
// Miscellaneous support functions for Cassandra
// Copyright (c) 1989, 1990 Jiro Nakamura
//
// misc.m
//
// RCS Information
// Revision Number->	$Revision: 2.3 $
// Last Revised->	$Date: 90/03/28 07:27:48 $
//
static char rcsid[] = "$Header: /User/jiro/Programming/Cassandra/src/RCS/misc.m,v 2.3 90/03/28 07:27:48 jiro Exp Locker: jiro $";

#import <stdio.h>
#import "cass.h"
#import <strings.h>
#import <appkit/Panel.h>       /* for NXRunAlertPanel */
#import <sys/file.h>
#import <libc.h>			/* for open(), close(), exit() */
#import "misc.h"
#import "errno.h"

// Function: 	fileOpen(filename, mode, message)
// Arguments:	char * filename -> name of file to be opened
//		char * mode	-> mode using fopen() (e.g. "r", "a+")
//		char * message	-> message to be shown if fopen() fails
// Description:	Opens the file <filename> with mode <mode>
//		using fopen(). If it is successful, a FILE *
//		is returned. If it fails, fileOpen tries
//		to create the file with creat() mode 0600.
//		If that fails too, it uses NXRunAlertPane()
//		to show <message>, then exit()s the program.
// Returns:	FILE * to open file
FILE *fileOpen(filename,mode,message)
char *filename, *mode, *message;
{
	FILE *fd, *fopen();

	#ifdef DEBUG
		fprintf(stderr,"fileOpened <%s> with mode %s\n",
				filename, mode);
	#endif

	if( (fd = fopen(filename, mode)) == NULL)
		{
		int ifd;

		fprintf(stderr,"%s: Uh...oh.... I (fileOpen) can't "
				"access %s with mode %s....\n"
				"%s: This is not a fatal error, "
				"I will try to create the file.\n",
				PROGNAME, filename,mode,
				PROGNAME);
		
		ifd = open( filename, O_CREAT, 0600);
		close(ifd);
		if( (fd = fopen(filename, mode)) == NULL)
			{
			NXRunAlertPanel("File Error",message,"Quit",NULL,NULL);
			exit(1);
			}
		}
	return fd;
}

// Seek file to this position
FILE *fileSeek(source, here, errormsg)
FILE *source;
int here;
char *errormsg;
{	
	#ifdef DEBUG
		fprintf(stderr,"Seeking to %d in fileSeek\n",here);
	#endif


	if(  here < 0)
		{
		fprintf(stderr,"%s: Error in fileSeek, "
			"cannot seek to negative number %d\n", 
			PROGNAME, here);
		NXRunAlertPanel("File Seek Error", 
			"Cannot seek to a negative number.",
			"OK",NULL,NULL);
		return NULL;
		}
	
	/* Seek to the correct position */
	if( fseek( source, (long) FILE_LEN * here, SEEK_SET) == -1) 	
		{		
		if( errno == EIO)
			{
			fprintf(stderr, "%s: I/O Error on read. ", PROGNAME);
			NXRunAlertPanel("Seek error", 
				"An IO error occured while seeking.",
				"OK",NULL,NULL);
			return NULL;
			}

		/* If we can't seek to the end because the file */
		/* is too short, then */
		/* seek to the end of file and add newlines*/			
		fseek(source, 0L, SEEK_END);		
		do
			{
			#ifdef DEBUG
				fprintf(stderr,"Looping: ftell(source) = "
					"%d \n",ftell(source));
			#endif

			fputc('\n', source);
			}
		while( ftell(source) < FILE_LEN * here + 5);
		
		/* Try again */
		if( fseek( source, (long) FILE_LEN * here, 0) == -1) 		
			{
			fprintf(stderr, 
			"%s: We have an error fseek()-ing to %d....\n"
			"%s: Error Numbers are as follows: errno = %d, EIO =%d"
			"%s: Calling procedure says: %s\n",
			PROGNAME, here, 
			PROGNAME, errno, EIO,
			PROGNAME, errormsg);

			NXRunAlertPanel( "Seek error", errormsg, 
				"Quit", NULL, NULL);
			fclose(source);
			exit(1);
			}
		}
	return source;
}

//
// fgetnum - read in a number into int format
//
int fgetnum(source)
FILE *source;
{
	int atoi();
	char str[20];
	
	fgets(str, 20, source);
	return( atoi(str));
}


